<?php
declare (strict_types = 1);

namespace app\api\controller\yuql;


use app\logic\yuql\SeckillLogic;
use app\services\yuql\FiltraServices;
use think\facade\Db;
use think\Request;

class Seckill
{

    /**
     * yuql
     * 商品列表展示
     * @return \think\response\Json
     */
    public function goodsList(Request $request){
        try {
            $params = $request->get();
            $data = SeckillLogic::goodsListLogic($params);
            return writeJson(20000,$data,'success');
        } catch (\Exception $e){
            return writeJson(1002,'',$e->getMessage());
        }
    }

    /**
     * yuql
     * 商品添加
     * @param Request $request
     * @return \think\response\Json
     */
    public function goodsAdd(Request $request){
        if (!$request->isPost()) return writeJson(1002,'',"请求方式不对");
        try {
            $params = $request->post();
            if (array_key_exists('data',$params)){
                $params = $request->post('data');
            }
            $result = SeckillLogic::goodsAddLogic($params);
            return writeJson(1001,$result,'添加成功');
        } catch (\Exception $e){
            return writeJson(1002,'',$e->getMessage());
        }
    }

    /**
     * yuql
     * 商品详情
     * @param Request $request
     * @return \think\response\Json
     */
    public function goodsDetail(Request $request){
        try {
            $id = $request->get('goods_id');
            $result = SeckillLogic::goodsDetailLogic($id);
            return writeJson(1001,$result,'success');
        } catch (\Exception $e){
            return writeJson(1002,'',$e->getMessage());
        }
    }


    /**
     * yuql
     * 商品订单
     * @param Request $request
     * @return \think\response\Json
     */
    public function goodsOrder(Request $request){
        if (!$request->isGet()) return writeJson(1002,'',"请求方式不对");
        Db::startTrans();
        try {
            $params = $request->get();
            $result = SeckillLogic::goodsBuyLogic($params);
            Db::commit();   //必须实现事务提交才能入库生成订单！！！
            return writeJson(1001,$result,'秒杀成功,订单生成');
        } catch (\Exception $e){
            Db::rollback();
            return writeJson(1002,'',$e->getMessage());
        }
    }

}
